package com.yj.suncity.activity;

import org.json.JSONObject;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.yj.suncity.R;
import com.yj.suncity.activity.shouye.CityHelpListActivity;
import com.yj.suncity.activity.shouye.DigouShopListActivity;
import com.yj.suncity.activity.shouye.DuihuanShopListActivity;
import com.yj.suncity.activity.shouye.OfferListActivity;
import com.yj.suncity.activity.shouye.RecentHuodongActivity;
import com.yj.suncity.async.ThreadUtils;
import com.yj.suncity.async.ThreadUtils.OnResultListener;
import com.yj.suncity.constant.ShareCon;
import com.yj.suncity.constant.URLCon;
import com.yj.suncity.publish.PublishAdvertiseActivity;
import com.yj.suncity.user.AlreadyPublishAdvActivity;
import com.yj.suncity.user.LoginActivity;
import com.yj.suncity.user.UpdatePwdActivity1;
import com.yj.suncity.user.UpdateShopInfoActivity;
import com.yj.suncity.user.UpdateUserInfoActivity;
import com.yj.suncity.user.UserDigouListActivity;
import com.yj.suncity.user.UserDuihuanListActivity;
import com.yj.suncity.user.UserOfferListActivity;
import com.yj.suncity.user.UserSeeAdvListActivity;
import com.yj.suncity.user.bean.EntBean;
import com.yj.suncity.utis.SharedPreferencesUtil;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class UserActivity extends Activity implements OnClickListener {

	private Context context;
	private SharedPreferencesUtil preferencesUtil = SharedPreferencesUtil
			.getInstance();
	private boolean isLogin;
	private boolean isShop;
	private String userName;
	private String userId;
	private String entId;// 企业ID
	private String userLogo;
	private String userPhone;
	private String userGrade;// 用户等级
	// private TextView mTxt_btn_outlogin;
	private ImageView mImg_user_logo;
	private TextView mTxt_user_name, mTxt_user_grade;
	private Button mBtn_regist_login;
	private LinearLayout mLay_linear_user;
	private View div_user_view;
	private DisplayImageOptions options;
	private ImageLoader imageLoader = ImageLoader.getInstance();
	private RelativeLayout mLay_update_user_info, mLay_update_shop_info,
			mLay_publish_offer, mLay_see_adv, mLay_publish_duihuan,
			mLay_user_offer, mLay_publish_digou, mLay_user_duihuan,
			mLay_publish_activity, mLay_user_digou, mLay_publish_help;
	private LinearLayout mLay_linear_shop, mLay_linear_person;

	private String[] grades = { "工兵", "连长", "师长", "军长", "司令" };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user);
		context = this;
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.android_layout_detail)
				.showImageForEmptyUri(R.drawable.android_layout_detail)
				.showImageOnFail(R.drawable.android_layout_detail)
				.cacheInMemory(true).cacheOnDisc(true)
				// .displayer(new RoundedBitmapDisplayer(10))// 设置成圆角图片
				.bitmapConfig(Bitmap.Config.RGB_565) // 设置图片的解码类型
				.build();
		findView();
	}

	private void findView() {
		div_user_view = findViewById(R.id.div_user_view);
		mBtn_regist_login = (Button) findViewById(R.id.btn_regist_login);
		mBtn_regist_login.setOnClickListener(this);
		mLay_linear_user = (LinearLayout) findViewById(R.id.lay_linear_user);

		mTxt_user_name = (TextView) findViewById(R.id.txt_user_name);
		// mTxt_user_phone = (TextView) findViewById(R.id.txt_user_phone);
		mImg_user_logo = (ImageView) findViewById(R.id.user_logo);
		mTxt_user_grade = (TextView) findViewById(R.id.txt_user_grade);

		mLay_update_user_info = (RelativeLayout) findViewById(R.id.lay_update_user_info);
		mLay_update_shop_info = (RelativeLayout) findViewById(R.id.lay_update_shop_info);

		findViewById(R.id.lay_update_pwd).setOnClickListener(this);
		findViewById(R.id.lay_publish_adv).setOnClickListener(this);
		findViewById(R.id.lay_publish_offer).setOnClickListener(this);
		findViewById(R.id.lay_see_adv).setOnClickListener(this);
		findViewById(R.id.lay_publish_duihuan).setOnClickListener(this);
		findViewById(R.id.lay_user_offer).setOnClickListener(this);
		findViewById(R.id.lay_publish_digou).setOnClickListener(this);
		findViewById(R.id.lay_user_duihuan).setOnClickListener(this);
		findViewById(R.id.lay_publish_activity).setOnClickListener(this);
		findViewById(R.id.lay_user_digou).setOnClickListener(this);
		findViewById(R.id.lay_publish_help).setOnClickListener(this);

		mLay_update_user_info.setOnClickListener(this);
		mLay_update_shop_info.setOnClickListener(this);

		mLay_linear_shop = (LinearLayout) findViewById(R.id.lay_linear_shop);
		mLay_linear_person = (LinearLayout) findViewById(R.id.lay_linear_person);

	}

	@Override
	protected void onResume() {
		init();
		super.onResume();
	}

	private void init() {
		isLogin = preferencesUtil.getBoolean(context, ShareCon.USERFILE,
				ShareCon.ISLOGIN, false);
		isShop = preferencesUtil.getBoolean(context, ShareCon.USERFILE,
				ShareCon.USERTYPE, false);
		if (isLogin) {
			div_user_view.setVisibility(View.GONE);
			userName = preferencesUtil.getString(context, ShareCon.USERFILE,
					ShareCon.USERNAME, "");
			userId = preferencesUtil.getString(context, ShareCon.USERFILE,
					ShareCon.USERID, "");
			userLogo = preferencesUtil.getString(context, ShareCon.USERFILE,
					ShareCon.USERLOGO, "");
			userPhone = preferencesUtil.getString(context, ShareCon.USERFILE,
					ShareCon.USERPHONE, "");
			entId = preferencesUtil.getString(context, ShareCon.USERFILE,
					ShareCon.SHOPID, "");
			userGrade = preferencesUtil.getString(context, ShareCon.USERFILE,
					ShareCon.USERINTEGRAL, "1");
			if (isShop && userLogo.equals("")) {// 如果是商家并且商家图片为空
				initShopInfo();
			}
			addLogin();
			if (isShop) {
				shopInfo();
			} else {
				userInfo();
			}
		} else {
			div_user_view.setVisibility(View.VISIBLE);
			outLogin();
			allUserInfo();
		}
	}

	private void initShopInfo() {// 为了一张商家图片
		String path = URLCon.用户 + "?action=getentinfobyentid&entid=" + entId;
		Log.d("", path);
		ThreadUtils threadUtils = new ThreadUtils();
		threadUtils.startThread(path, new OnResultListener() {
			@Override
			public void jsonResult(JSONObject json) {
				EntBean entBean = new EntBean(json);
				if (entBean.getSuccess().equals("1")) {
					preferencesUtil.putString(context,
							ShareCon.USERFILE, ShareCon.USERLOGO,
							entBean.getEnt_logo());
					imageLoader.displayImage(
							URLCon.HOST + entBean.getEnt_logo(),
							mImg_user_logo, options);
				}
			}
		});
	}

	// 全部用户登录按钮
	private void allUserInfo() {
		mLay_linear_shop.setVisibility(View.VISIBLE);
		mLay_linear_person.setVisibility(View.VISIBLE);
		mLay_update_shop_info.setVisibility(View.VISIBLE);
		mLay_update_user_info.setVisibility(View.VISIBLE);
	}

	// 个人用户登录按钮
	private void userInfo() {
		mLay_linear_shop.setVisibility(View.GONE);
		mLay_linear_person.setVisibility(View.VISIBLE);
		mLay_update_shop_info.setVisibility(View.GONE);
		mLay_update_user_info.setVisibility(View.VISIBLE);
	}

	// 商户登录按钮
	private void shopInfo() {
		mLay_linear_shop.setVisibility(View.VISIBLE);
		mLay_linear_person.setVisibility(View.VISIBLE);
		mLay_update_shop_info.setVisibility(View.VISIBLE);
		mLay_update_user_info.setVisibility(View.GONE);
	}

	private void addLogin() {

		mLay_linear_user.setVisibility(View.VISIBLE);
		mBtn_regist_login.setVisibility(View.GONE);
		mTxt_user_name.setText(userName);
		// mTxt_user_phone.setText(userPhone);
		mTxt_user_grade.setText(grades[Integer.valueOf(userGrade) - 1]);
//		if (!isShop) {
			imageLoader.displayImage(URLCon.HOST + userLogo, mImg_user_logo,
					options);
//		}
	}

	private void outLogin() {
		preferencesUtil.putBoolean(context, ShareCon.USERFILE,
				ShareCon.ISLOGIN, false);
		isLogin = false;
		mLay_linear_user.setVisibility(View.GONE);
		mBtn_regist_login.setVisibility(View.VISIBLE);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		 case R.id.lay_update_pwd:
		 if (isLogin) {
			 Intent intent=new Intent(context, UpdatePwdActivity1.class);
			 intent.putExtra("titlename", "修改密码");
			 intent.putExtra("userphone", userPhone);
			 intent.putExtra("flag", 1);
			 startActivity(intent);
		} else {
			startLoginActivity();
		}
		 break;
		case R.id.btn_regist_login:
			startActivity(new Intent(this, LoginActivity.class));
			break;
		case R.id.lay_update_user_info:// 修改个人用户信息
			if (isLogin) {
				Intent intent = new Intent(context,
						UpdateUserInfoActivity.class);
				intent.putExtra("userid", userId);
				startActivity(intent);
			} else {
				startLoginActivity();
			}
			break;
		case R.id.lay_see_adv:// 已看广告
			if (isLogin) {
				Intent intent = new Intent(context,
						UserSeeAdvListActivity.class);
				intent.putExtra("userid", userId);
				intent.putExtra("titlename", "已看广告");
				startActivity(intent);
			} else {
				startLoginActivity();
			}

			break;
		case R.id.lay_user_offer:// 已接悬赏
			if (isLogin) {
				Intent intent = new Intent(context, UserOfferListActivity.class);
				intent.putExtra("userid", userId);
				intent.putExtra("titlename", "已接悬赏");
				startActivity(intent);
			} else {
				startLoginActivity();
			}
			break;
		case R.id.lay_user_duihuan:// 已购买商品
			if (isLogin) {
				Intent intent = new Intent(context,
						UserDuihuanListActivity.class);
				intent.putExtra("userid", userId);
				intent.putExtra("titlename", "已购买商品");
				startActivity(intent);
			} else {
				startLoginActivity();
			}
			break;
		case R.id.lay_user_digou:// 已抵购商品
			if (isLogin) {
				Intent intent = new Intent(context, UserDigouListActivity.class);
				intent.putExtra("userid", userId);
				intent.putExtra("titlename", "已抵购商品");

				startActivity(intent);
			} else {
				startLoginActivity();
			}
			break;
		// ===========================================================
		case R.id.lay_update_shop_info:// 修改商家信息
			if (isLogin) {
				Intent intent = new Intent(context,
						UpdateShopInfoActivity.class);
				intent.putExtra("userid", userId);
				intent.putExtra("entid", entId);
				intent.putExtra("titlename", "修改商户资料");
				intent.putExtra("flag", 1);
				startActivity(intent);
			} else {
				startLoginActivity();
			}
			break;
		case R.id.lay_publish_adv:// 已发布广告
			if (isLogin) {
				Intent intent = new Intent(context,
						PublishAdvertiseActivity.class);
				intent.putExtra("userid", userId);
				intent.putExtra("titlename", "已发布广告");
				startActivity(intent);
			} else {
				startLoginActivity();
			}
			break;
		case R.id.lay_publish_offer:// 已发布悬赏
			if (isLogin) {
				Intent intent = new Intent(context, OfferListActivity.class);
				intent.putExtra("userid", userId);
				intent.putExtra("flag", 2);
				intent.putExtra("titlename", "已发布悬赏");
				startActivity(intent);
			} else {
				startLoginActivity();
			}
			break;
		case R.id.lay_publish_duihuan:// 已发布商品
			if (isLogin) {
				Intent intent = new Intent(context,
						DuihuanShopListActivity.class);
				intent.putExtra("userid", userId);
				intent.putExtra("titlename", "已发布商品");
				startActivity(intent);
			} else {
				startLoginActivity();
			}
			break;
		case R.id.lay_publish_digou:// 已发布抵购商品取消了
			if (isLogin) {
				Intent intent = new Intent(context, DigouShopListActivity.class);
				intent.putExtra("userid", userId);
				intent.putExtra("titlename", "已发布抵购商品");
				startActivity(intent);
			} else {
				startLoginActivity();
			}
			break;
		case R.id.lay_publish_activity:// 已发布活动
			if (isLogin) {
				Intent intent = new Intent(context, RecentHuodongActivity.class);
				intent.putExtra("userid", userId);
				intent.putExtra("titlename", "已发布活动");
				startActivity(intent);
			} else {
				startLoginActivity();
			}
			break;
		case R.id.lay_publish_help:// 已发布同城帮帮
			if (isLogin) {
				Intent intent = new Intent(context, CityHelpListActivity.class);
				intent.putExtra("userid", userId);
				startActivity(intent);
			} else {
				startLoginActivity();
			}
			break;
		default:
			break;
		}
	}

	private void startLoginActivity() {
		startActivity(new Intent(context, LoginActivity.class));
	}

}
